#ifndef CPP_VIEWPAGER_H
#define CPP_VIEWPAGER_H

#include <vector>
#include <widget/View.h>
#include <widget/ViewGroup.h>
#include <utils/Timer.h>

class Context;

class ViewPage : public ViewGroup {
   public:
    ViewPage();
    ~ViewPage();
};

//@brief 容器
//@author cvte
//@description 翻页控件
class ViewPager : public ViewGroup {
   public:
    class OnViewPageChangeListener {
       public:
        virtual void onViewPageChanged(ViewPager& viewPager, int pageIndex);
    };

    typedef std::vector<ViewPage*> viewPageVector;
    ViewPager();
    ~ViewPager();

    View* findViewById(int id);

    //@method 增加页
    void addViewPage(ViewPage* viewPage);
    //@method 删除页
    void removeViewPage(ViewPage* viewPage);
    //@method 判断是否存在页
    bool isContainViewPage(ViewPage* viewPage);
    //@method 通知当前页内容有变化，会更新对应的Canvas显示内容
    void notifyCurPageContentIsChanged();

    void setOnViewPageChangeListener(OnViewPageChangeListener& listener);

   protected:
    viewPageVector mViewPages;
    Canvas* mContentCanvas = nullptr;

   private:
    int mTargetScrollGap = 0; //目标距离
    int mCurrentPageIndex = 0; //当前页索引
    Timer mSettlingTimer;
    OnViewPageChangeListener* mOnViewPageChangeListener = nullptr;

    void startSettlingTimer();
    void stopSettlingTimer();
    void onAttachedToWindow();
    void onDetachedFromWindow();
    void onDraw(Canvas& canvas);
    void onDrawContent(); //绘制内容
    void onTouched(MotionEvent& event);
    void onScrolled(MotionEvent& event);
};

#endif /* CPP_VIEWPAGER_H */